package leetcode_1100;


/**
 *@author 周杨 
 *StatisticsFromALargeSample_1093 给一个数组 count[i]表示 有count[i]个值为i的元素 
 *求真正数组里的最大值 最小值 中位数 平均值 和 众数
 *describe:常规题目
 *2019年7月23日 下午17:23 
 */
public class StatisticsFromALargeSample_1093 {
	public static void main(String[] args) {
		StatisticsFromALargeSample_1093 test = new StatisticsFromALargeSample_1093();
		Object res = test.sampleStats(new int[] {0,1,3,4,0});
	}
	
	public double[] sampleStats(int[] count) {
        double[] res = new double[5];
        int i = 0;
        while(i < count.length && count[i] == 0) {
        	++i;
        }
        res[0] = i;//min
        i = count.length-1;
        while(i >= 0 && count[i] == 0) {
        	--i;
        }
        res[1] = i;//max
        int c = 0;//记录出现了多少个数
        double sum = 0d;
        int maxIndex = 0,max = 0;
        for(int j=0;j<count.length;++j) {
        	c += count[j];
        	sum += j*count[j];
        	if(count[j] > max) {
        		maxIndex = j;
        		max = count[j];
        	}
        }
        
        res[2] = sum/c;//mean
        
        if(c % 2 == 1) {//odd
        	int index = (c+1)/2;
        	for(int j=0;j<count.length;++j) {
        		if(count[j] >= index) {
        			res[3] = j;
        			break;
        		}else {
        			index -= count[j];
        		}
        	}
        }else {//even
        	int index = c/2;
        	for(int j=0;j<count.length;++j) {
        		if(count[j] >= index) {
        			if(count[j] == index) {//找到左
        				int k = j+1;
        				while(k < count.length && count[k] == 0) {
        					++k;
        				}
        				res[3] = ((double)(j+k))/2;
        			}else {
        				res[3] = j;
        			}
        			break;
        		}else {
        			index -= count[j];
        		}
        	}
        }
        
        res[4] = maxIndex;
        return res;
    }
}
